package com.online.shopping.zuul;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class WebFilter extends ZuulFilter {

	/**
	 * true: 执行run()
	 * false: 不执行run()
	 * @return
	 */
	@Override
	public boolean shouldFilter() {
		return true;
	}

	/**
	 * 过滤器的主方法：将敏感的请求头信息重新加到请求中
	 * @return
	 * @throws ZuulException
	 */
	@Override
	public Object run() throws ZuulException {
		RequestContext requestContext = RequestContext.getCurrentContext();
		HttpServletRequest request = requestContext.getRequest();

		String token = request.getHeader("anthorization");
		if(token!=null && !"".equals(token)){
			requestContext.addZuulRequestHeader("anthorization",token);
		}
		return null;
	}

	@Override
	public String filterType() {
		return FilterConstants.PRE_TYPE;
	}

	/**
	 * 配置过滤器的顺序
	 * @return
	 */
	@Override
	public int filterOrder() {
		return 0;
	}
}
